/***
 * Excerpted from "Stripes: and Java Web Development is Fun Again",
 * published by The Pragmatic Bookshelf.
 * Copyrights apply to this code. It may not be used to create training material, 
 * courses, books, articles, and the like. Contact us if you are in doubt.
 * We make no guarantees that this code is fit for any purpose. 
 * Visit http://www.pragmaticprogrammer.com/titles/fdstr for more book information.
***/
package stripesbook.view;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.controller.StripesFilter;
import org.stripesbook.rayures.view.ViewTemplate;
import org.stripesbook.rayures.view.stripes.SLink;
import org.stripesstuff.plugin.security.SecurityInterceptor;
import stripesbook.action.BaseActionBean;
import stripesbook.action.LogoutActionBean;
import stripesbook.action.user.ContactListActionBean;
import stripesbook.action.user.MessageComposeActionBean;
import stripesbook.action.user.UserListActionBean;
import stripesbook.action.user.folders.MessageListActionBean;

public class Menu extends ViewTemplate<Menu> {
  public boolean getDisplayMenu() {
    return ((BaseActionBean) getViewContext().getActionBean()).getUser() != null;
  }
  public Section getCurrentSection() {
    return ((BaseActionBean) getViewContext().getActionBean()).getCurrentSection();
  }
  public List<Section> getAllowedSections() throws Exception {
    List<Section> list = new ArrayList<Section>();
    for (Section section : getSections()) {
      Class<? extends ActionBean> bean = section.getBeanclass();
      if (isAllowed(bean)) {
        list.add(section);
      }
    }
    return list;
  }
  private boolean isAllowed(Class<? extends ActionBean> beanClass) throws Exception {
    ActionBean actionBean = StripesFilter.getConfiguration().getObjectFactory().newInstance(beanClass);
    actionBean.setContext(getViewContext().getActionBean().getContext());
    HttpServletRequest request = getViewContext().getRequest();
    org.stripesstuff.plugin.security.SecurityManager securityManager =
      (org.stripesstuff.plugin.security.SecurityManager)
      request.getAttribute(SecurityInterceptor.SECURITY_MANAGER);
    if (securityManager == null) {
      return true;
    }
    Method handler = StripesFilter.getConfiguration().getActionResolver().getDefaultHandler(beanClass);
    return Boolean.TRUE.equals(securityManager.getAccessAllowed(actionBean, handler));
  }
  public SLink getLinkFor(Section section) {
    return tag(SLink.class).beanclass(section.getBeanclass()).cssClass("sectionLink")
      .label(bundle.get(section.getTextKey()));
  }
  public Section[] getSections() {
    return Section.values();
  }

  public enum Section {
    MessageList(MessageListActionBean.class),
    ContactList(ContactListActionBean.class),
    Compose(MessageComposeActionBean.class),
    UserList(UserListActionBean.class),
    Logout(LogoutActionBean.class);

    private String textKey;
    private Class<? extends ActionBean> beanclass;
    Section(Class<? extends ActionBean> beanclass) {
      this.textKey = "section." + name();
      this.beanclass = beanclass;
    }
    public String getTextKey() { return textKey; }
    public Class<? extends ActionBean> getBeanclass() { return beanclass; }
  }
}
